package util.baidu;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class BaiduMusicUtil {

	private static final String BAIDU_MUSIC_URL = "http://tingapi.ting.baidu.com/v1/restserver/ting?format=json";
	
	public static final Map<String, String> MUSIC_TYPE = new HashMap<String, String>();
	
	static{
		MUSIC_TYPE.put("1", "新歌榜");
		MUSIC_TYPE.put("2", "热歌榜");
		MUSIC_TYPE.put("11", "摇滚榜");
		MUSIC_TYPE.put("12", "爵士");
		MUSIC_TYPE.put("16", "流行");
		MUSIC_TYPE.put("21", "欧美金曲榜");
		MUSIC_TYPE.put("22", "经典老歌榜");
		MUSIC_TYPE.put("23", "情歌对唱榜");
		MUSIC_TYPE.put("24", "影视金曲榜");
		MUSIC_TYPE.put("25", "网络歌曲榜");
	}
	
	/**
	 * 根据类型获取百度音乐列表
	 * @param type 1-新歌榜,2-热歌榜,11-摇滚榜,12-爵士,16-流行,21-欧美金曲榜,22-经典老歌榜,23-情歌对唱榜,24-影视金曲榜,25-网络歌曲榜
	 * @param size 返回条目数量
	 * @param offset 获取偏移
	 * @return 返回获取的对象
	 * @anthor billdonwey
	 */
	public static JSONObject getMusicList(String type, Integer size, Integer offset) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.billboard.billList&type=" + type + "&size=" + size + "&offset=" + offset;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 根据关键字在百度搜索歌曲
	 * @param query 搜索关键字
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject searchMusic(String query) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.search.catalogSug&query=" + query;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 根据歌曲id获取歌曲详情
	 * @param songid 歌曲id
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getMusicDetail(String songid) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.song.play&songid=" + songid;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 根据歌曲id获取歌词
	 * @param songid 歌曲id
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getMusicLRC(String songid) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.song.lry&songid=" + songid;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 根据歌曲id获取推荐列表
	 * @param song_id 歌曲id
	 * @param num 获取数目
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getRecommandSongList(String song_id, String num) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.song.getRecommandSongList&song_id=" + song_id + "&num=" + num;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 获取歌曲下载信息
	 * @param songid 歌曲id
	 * @param bit 码率:24, 64, 128, 192, 256, 320 ,flac
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject downloadMusicInfo(String songid, String bit) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.song.downWeb&songid=" + songid + "&bit=" + bit + "&_t=" + (new Date().getTime());
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}
	
	/**
	 * 获取歌手列表
	 * @param order 未知参数（反正对结果有影响）
	 * @param offset 偏移量
	 * @param limit 返回结果的数量
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getSingerList(String order, String offset, String limit) {
		String url = BAIDU_MUSIC_URL + "&method=baidu.ting.artist.get72HotArtist&order=" + order + "&offset=" + offset + "&limit=" + limit;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}

	/**
	 * 获取歌手详细信息
	 * @param tinguid 歌手ting id
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getSingerInfo(String tinguid) {
		String url = BAIDU_MUSIC_URL + "method=baidu.ting.artist.getInfo&tinguid=" + tinguid;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}

	/**
	 * 获取歌手歌曲列表
	 * @param tinguid 歌手ting id
	 * @param limits 返回条目数量
	 * @param order 未知参数（反正对结果有影响）
	 * @return
	 * @anthor billdonwey
	 */
	public static JSONObject getSingerSongList(String tinguid, String limits, String order) {
		String url = BAIDU_MUSIC_URL + "method=baidu.ting.artist.getSongList&tinguid=" + tinguid + "&limits=" + limits + "&order=" + order;
		JSONObject out = NetworkCaughtUtil.sendGETReturnJSON(url);
		return out;
	}

}
